﻿// CLoginDlg.cpp: 实现文件
//

#include "pch.h"
#include "EmployeeManagement.h"
#include "afxdialogex.h"
#include "CLoginDlg.h"

// CLoginDlg 对话框

IMPLEMENT_DYNAMIC(CLoginDlg, CDialogEx)

CLoginDlg::CLoginDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_LOGIN_DIALOG, pParent)
	, m_jobid(_T(""))
	, m_name(_T(""))
	, m_password(_T(""))
{
	ReadConfig();//读取配置文件

}

CLoginDlg::~CLoginDlg()
{
}

void CLoginDlg::WriteConfig()
{
	LPWSTR strPath = new WCHAR[MAX_PATH];
	GetModuleFileName(NULL, strPath, MAX_PATH);//获取可执行文件路径
	CString strIniPath(strPath);
	strIniPath = strIniPath.Left(strIniPath.ReverseFind('\\')) + _T("\\config.ini");//获取ini文件路径
	WritePrivateProfileString(_T("Login"), _T("JobID"), m_jobid, strIniPath);
	WritePrivateProfileString(_T("Login"), _T("Name"), m_name, strIniPath);
	WritePrivateProfileString(_T("Login"), _T("Password"), m_password, strIniPath);
}

void CLoginDlg::ReadConfig()
{
	LPWSTR strPath = new WCHAR[MAX_PATH];
	GetModuleFileName(NULL, strPath, MAX_PATH);//获取可执行文件路径
	CString strIniPath(strPath);
	strIniPath = strIniPath.Left(strIniPath.ReverseFind('\\')) + _T("\\config.ini");//获取ini文件路径
	GetPrivateProfileString(_T("Login"), _T("JobID"), _T(""), m_jobid.GetBuffer(MAX_PATH), MAX_PATH, strIniPath);
	GetPrivateProfileString(_T("Login"), _T("Name"), _T(""), m_name.GetBuffer(MAX_PATH), MAX_PATH, strIniPath);
	GetPrivateProfileString(_T("Login"), _T("Password"), _T(""), m_password.GetBuffer(MAX_PATH), MAX_PATH, strIniPath);
}

void CLoginDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_JOBID, m_jobid);
	DDX_Text(pDX, IDC_EDIT_NAME, m_name);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_password);
}


BEGIN_MESSAGE_MAP(CLoginDlg, CDialogEx)
	ON_BN_CLICKED(IDOK, &CLoginDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CLoginDlg 消息处理程序


void CLoginDlg::OnBnClickedOk()
{
	UpdateData(TRUE);//更新数据

	if (m_jobid == "1001" && m_password == "123456" && m_name == "admin")
	{
		WriteConfig();//写入配置文件
		CDialogEx::OnOK();
	}
	else
		MessageBox(_T("工号、密码或姓名错误！\n请重新输入！"), _T("提示"), MB_OK | MB_ICONINFORMATION);
	//清空
	SetDlgItemText(IDC_EDIT_JOBID, _T(""));
	SetDlgItemText(IDC_EDIT_PASSWORD, _T(""));
	SetDlgItemText(IDC_EDIT_NAME, _T(""));
	//设置光标位置
	GetDlgItem(IDC_EDIT_JOBID)->SetFocus();
}
